<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class UserEditableYes extends UserYes
{

    protected $edit = false;

    function __construct()
    {
        parent::__construct();
        $this->editable = true;
    }

    protected $form_fiscal_id;
    protected $form_name;
    protected $form_first_surname;
    protected $form_second_surname;
    protected $form_username;

    // protected $form_password;


    function setEditar( $edit )
    {
        $this->edit = $edit;
    }

    function setFormData( $fiscal_id, $name, $first_surname, $second_surname,
            $username, $password, $repassword )
    {
        $this->form_fiscal_id = $fiscal_id;
        $this->form_name = $name;
        $this->form_first_surname = $first_surname;
        $this->form_second_surname = $second_surname;
        $this->form_username = $username;

        if ( empty( $name ) ) $this->errors[ ] = 'name';

        if ( empty( $first_surname ) ) $this->errors[ ] = 'first_surname';

        if ( empty( $fiscal_id ) ) $this->errors[ ] = 'fiscal_id';

        else if ( !UserNo::control_nif( $fiscal_id ) && !UserNo::control_nie( $fiscal_id ) )
                $this->errors[ ] = 'fiscal_id';

        else {
            $res = MyLink::$default->get_rows( 'SELECT id FROM users WHERE fiscal_id=' . MyLink::quote( $fiscal_id ) . ' AND id<>' . $this->id,
                            true )->getField( 0 );
            if ( !empty( $res ) ) $this->errors[ ] = 'fiscal_id';
        }

        if ( empty( $username ) ) $this->errors[ ] = 'username';

        $res = MyLink::$default->get_rows( 'SELECT id FROM users WHERE username=' . MyLink::quote( $username ) . ' AND id<>' . $this->id,
                        true )->getField( 0 );
        if ( !empty( $res ) ) $this->errors[ ] = 'username';

        if ( empty( $password ) ) $this->errors[ ] = 'password';

        if ( empty( $repassword ) ) $this->errors[ ] = 'repassword';

        if ( $password != $repassword ) $this->errors[ ] = 'repassword';
    }

    function handleGET( Event $e )
    {
        if ( $e->tag == 'editar' ) {
            $this->_setEditar( true );

            $this->_setFormData( $this->fiscal_id, $this->name,
                    $this->first_surname, $this->second_surname,
                    $this->username, $this->password, $this->password );
            $e->cancelBubble();
        }
    }

    function handlePOST( Event $e )
    {


        if ( $e->tag == 'editar' ) {
            if ( $_POST[ 'quote_update' ] == '1' ) {

                $_POST[ 'name' ] = trim( preg_replace( '/\s+?/', ' ',
                                $_POST[ 'name' ] ) );
                $_POST[ 'first_surname' ] = trim( preg_replace( '/\s+?/', ' ',
                                $_POST[ 'first_surname' ] ) );
                $_POST[ 'second_surname' ] = trim( preg_replace( '/\s+?/', ' ',
                                $_POST[ 'second_surname' ] ) );
                $_POST[ 'fiscal_id' ] = strtr( $_POST[ 'fiscal_id' ],
                        array( '.' => '', '-' => '', ' ' => '' ) );


                $this->_setFormData( $_POST[ 'fiscal_id' ], $_POST[ 'name' ],
                        $_POST[ 'first_surname' ], $_POST[ 'second_surname' ],
                        $_POST[ 'username' ], $_POST[ 'password' ],
                        $_POST[ 'repassword' ] );
            }
            else $this->_setEditable( false );

            $e->cancelBubble();
        }
        else if ( $e->tag == 'eliminar' ) {
            $this->delete();
        }
        else parent::handlePOST( $e );
    }

    function doPOST( Event $e )
    {
        if ( $e->tag == 'eliminar' ) $this->delete();

        else if ( $e->tag == 'editar' ) {
            $this->update();
            $this->_setEditar( false );
        }
        else parent::doPOST( $e );
    }

    function update()
    {
        $sql = 'UPDATE users SET fiscal_id=' . MyLink::quote( $_POST[ 'fiscal_id' ] ) . ', name=' . $_POST[ 'name' ] . ', first_surname=' . $_POST[ 'first_surname' ] . ', second_surname=' . $_POST[ 'second_surname' ] . ', username=' . $_POST[ 'username' ] . ', password=' . $_POST[ 'password' ] . ' WHERE id=' . $this->id;
        MyLink::$default->execute( $sql );
    }

    function delete()
    {
        
    }

}

?>
